package aom.ju.ss.view.user.shop.management.goods.addGoods

import android.content.Intent
import android.view.WindowManager
import aom.ju.ss.R
import aom.ju.ss.databinding.NewCreateGoodsActivityBinding
import aom.ju.ss.mode.AddGoodsData
import aom.ju.ss.mode.ClassModeDataInfoThree
import aom.ju.ss.utils.VG
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.dialog.AddGoodsDialog
import aom.ju.ss.view.dialog.AddGoodsDialogCall


/**
 * 添加商品
 */
class NewCreateGoodsActivity : BaseActivity() {
    private lateinit var binding: NewCreateGoodsActivityBinding
    val arrtv = arrayOf("", "普通商品", "砍价商品", "", "易货商品")//"促销商品" , "抢券商品"


    companion object {
        var photo_album = arrayListOf<String>()
        var content_images = arrayListOf<String>()

        var upmode = AddGoodsData()
    }

    var head: AddGoodsHead? = null
    var yhUserInfo: AddGoodsYH? = null
    var attrFragment: AddGoodsAttrFragment? = null

    var key = arrayListOf<String>()
    var value = arrayListOf<String>()
    override fun init() {
        binding = viewBinding as NewCreateGoodsActivityBinding
        initData()
        initView()
        binding.info = upmode
        binding.classTv.setOnClickListener {
            AddGoodsDialog(activity!!, object : AddGoodsDialogCall {
                override fun call(p: Int) {
                    upmode.type = p.toString()
                    binding.info = upmode
                    binding.classTv.text = arrtv[p]
                    yhUserInfo!!.binding.rootView.VG(p == 4)
                }
            })
        }


        binding.shangjia.setOnClickListener {
            key.add("name")
            key.add("category_id_one")
            key.add("category_id_two")
            key.add("category_id_three")
            key.add("market_price")
            key.add("type")
            key.add("photo_album[]")
            key.add("is_afhalen")
            key.add("content_images[]")


            key.add("skus[0][name]")
            key.add("skus[0][image]")
            key.add("skus[0][price]")
            key.add("skus[0][inventory]")



            value.add("name")
            value.add(upmode.category_id_one)
            value.add(upmode.category_id_two)
            value.add(upmode.category_id_three)
            value.add("1")
            value.add("1")
            value.add("http://www.www.jpg")
            value.add("1")
            value.add("http://www.www.jpg")

            value.add("大萨达撒大所")
            value.add("http://www.www.jpg")
            value.add("1")
            value.add("1000")

            OKHttpUtils(activity)
                .SetApiUrl(LP_API.save_goods)
                .SetKey(*key.toArray(arrayOf<String>()))
                .SetValue(*value.toArray(arrayOf<String>()))
                .POST(this)
        }
    }

    private fun initData() {
        head = supportFragmentManager.findFragmentById(R.id.head_fragment) as AddGoodsHead
        yhUserInfo = supportFragmentManager.findFragmentById(R.id.yh_fragment) as AddGoodsYH
        attrFragment =
            supportFragmentManager.findFragmentById(R.id.attr_fragment) as AddGoodsAttrFragment
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)

        intent?.getStringExtra("good_id")?.let {
            upmode.id = it
        }
        intent?.getStringExtra("goodsType")?.let {
            upmode.type = it
        }

    }

    override val lay: Int = R.layout.new_create_goods_activity


    private fun initView() {
        if (upmode.id != "") {// 编辑商品
            binding.title.title.text = "编辑商品"
        }
        binding.classTv.text = arrtv[upmode.type.toInt()]
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            if (requestCode == 1993) {
                val cates: MutableList<ClassModeDataInfoThree>? =
                    data?.getParcelableArrayListExtra("data")
                cates?.let {
                    for (childBean in it) {
                        upmode.category_id_one = childBean.idone
                        upmode.category_id_two = childBean.idtwo
                        upmode.category_id_three = childBean.id
                    }
                }
            }
        }
    }

}
